﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InternalElementViewModel.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   UI Representation of an Internal Element <see cref="InternalElementType" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.ViewModels.CAEXObjectViewModels
{
    using System.IO;
    using System.Linq;

    using CAEX_ClassModel;

    /// <summary>
    ///   UI Representation of an Internal Element <see cref="InternalElementType" />
    /// </summary>
    public class InternalElementViewModel : CAEXObjectViewModel<InternalElementType>
    {
        #region Constructors and Destructors

        /// <summary>
        ///  Initializes a new instance of the <see cref="InternalElementViewModel"/> class. 
        /// </summary>
        /// <param libraryName="element">
        /// a CAEX - InternalElement 
        /// </param>
        public InternalElementViewModel(InternalElementType element)
            : base(element)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   DisplayName of the CAEX-Element in the UI used in Headers and Labels
        /// </summary>
        public static new string DisplayName
        {
            get
            {
                return "InternalElement";
            }
        }

        /// <summary>
        ///   Thew Name of the Role, which belongs to the RoleRequirements of the Internal Element
        /// </summary>
        public string RoleRequirementName
        {
            get
            {
                var name = string.Empty;
                if (this.CaexObject != null)
                {
                    if (this.CaexObject.RoleRequirements.Exists)
                    {
                        if (this.CaexObject.RoleRequirements[0].RefBaseRoleClassPath.Exists())
                        {
                            name = Path.GetFileName(this.CaexObject.RoleRequirements[0].RefBaseRoleClassPath.Value);
                        }
                    }
                }

                return name;
            }
        }

        #endregion

        #region Public Indexers

        /// <summary>
        /// overrides IDataErrorInfo with a validation rule for checking of multiple IDs assigned to InternalElement
        /// </summary>
        /// <param libraryName="propertyName">
        /// </param>
        /// <returns>
        /// The System.String. 
        /// </returns>
        public override string this[string propertyName]
        {
            get
            {
                var result = base[propertyName];
                if (result != null)
                {
                    return result;
                }

                this.IsValid = false;

                if (this.LibraryCaexObject is InstanceHierarchyType && this.CaexObject.ID.Exists())
                {
                    var count =
                        (this.LibraryCaexObject as InstanceHierarchyType).InternalElement.Cast<CAEXObject>().Count(
                            ie => ie.ID.Exists() && ie.ID.Value == this.CaexObject.ID.Value);

                    if (count > 1)
                    {
                        return "Multiple Internal Element";
                    }
                }

                this.IsValid = true;
                return null;
            }
        }

        #endregion
    }
}